package top.yamhk.agent.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述：功能描述
 *
 * @author 庆 HongKong
 * @date 2021/5/31-17:14
 */
@Slf4j
public class DataMapper {
    public static String translateToPo(String key) {
        return buildPoStr(key);
    }

    /**
     * 数据库字段转Po
     *
     * @param key key
     * @return StringBuilder
     */
    public static String buildPoStr(String key) {
        StringBuilder stringBuilder = new StringBuilder();
        final String[] split = key.split("_");
        for (int i = 0; i < split.length; i++) {
            if (StringUtils.isEmpty(split[i])) {
                continue;
            }
            if (i != 0) {
                stringBuilder.append(split[i].substring(0, 1).toUpperCase()).append(split[i].substring(1).toLowerCase());
            } else {
                stringBuilder.append(split[i].toLowerCase());
            }
        }
        return stringBuilder.toString();
    }

    public static String getSqlForCount(Class<?> clazz, String tableName) {
        return "select count(1) from " + tableName + "";
    }

//    /**
//     * 数据转换
//     *
//     * @param mapList mapList
//     * @param clazz   clazz
//     * @return HashMap
//     */
//    public List doDataReMapper(List<Map<String, Object>> mapList, Class<?> clazz) {
//        return doGetPoMap(mapList).stream()
//                .map(e -> JsonUtils.GSON.fromJson(JsonUtils.GSON.toJson(e), clazz))
//                .collect(Collectors.toList());
//
//    }

    /**
     * 获取插入语句
     *
     * @param poClazz   poClazz
     * @param tableName tableName
     * @return String
     */
    public static String getSqlForPoSave(Class<?> poClazz, String tableName) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" insert into ").append(tableName);
        //
        stringBuilder.append(" ( ");
        for (Field declaredField : poClazz.getDeclaredFields()) {
            if (declaredField.getName().contains("$")) {
                continue;
            }
            final String str = buildDbStr(declaredField.getName());
            stringBuilder.append(str).append(",");
        }
        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
        stringBuilder.append(" ) ");
        //
        stringBuilder.append("values");
        //
        stringBuilder.append(" ( ");
        for (Field declaredField : poClazz.getDeclaredFields()) {
            if (declaredField.getName().contains("$")) {
                continue;
            }
            if (!"ID".equalsIgnoreCase(declaredField.getName())) {
                stringBuilder.append("#");
            }
            stringBuilder.append(declaredField.getName()).append(",");
        }
        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
        stringBuilder.append(")");
        return stringBuilder.toString();
    }

    /**
     * 构建数据库字段
     *
     * @param key key
     * @return String
     */
    public static String buildDbStr(String key) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < key.length(); i++) {
            final char c = key.charAt(i);
            stringBuilder.append(c >= 'A' && c <= 'Z' ? "_" : "");
            stringBuilder.append(c);
        }
        return stringBuilder.toString().toUpperCase();
    }

    /**
     * 数据转换
     *
     * @param mapList mapList
     * @return HashMap
     */
    public List<Map<String, Object>> doGetPoMap(List<Map<String, Object>> mapList) {
        return mapList.stream()
                .map(this::getDataReMapperIntoCurrentMap)
                .collect(Collectors.toList());
    }

    /**
     * 重新映射
     *
     * @param stringObjectMap stringObjectMap
     * @return HashMap
     */
    public Map<String, Object> getDataReMapperIntoCurrentMap(Map<String, Object> stringObjectMap) {
        final Map<String, Object> stringObjectHashMap = new HashMap();
        stringObjectMap.forEach((key, value) -> stringObjectHashMap.put(buildPoStr(key), value == null ? "" : value));
        return stringObjectHashMap;
    }

    /**
     * sql参数填充
     *
     * @param param   param
     * @param realSql realSql
     * @return String
     */
    public String getSqlMake(Map<String, Object> param, String realSql) {
        final String[] sql = {realSql};
        final String spi = "#";
        param.forEach((key, value) -> {
            String replacement = value.toString().contains(":") ? ("to_date('" + value.toString().substring(0, 19) + "','yyyy-mm-dd hh24:MI:ss')") : ("'" + value + "'");
            sql[0] = sql[0].replaceAll(spi + key, replacement);
        });
        if (sql[0].contains(spi)) {
            log.error("[{}]-sql参数缺失-[{}]", "==>", sql[0]);
            return "";
        }
        return sql[0];
    }
}